package ming.controller;

import com.study.ming.entity.WeatherPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yh
 * @date 2022/10/12 12:08
 **/
@RestController
public class HomeController {

    @GetMapping("/getByCity")
    public WeatherPojo getWeatherByCity(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
        return new WeatherPojo(id, "news", "haha", "33");
    }
//    @GetMapping("/getByCity2")
//    public WeatherPojo getWeatherByCity2(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
//        return restTemplate.getForObject("http://weather/getByCity?id="+id, WeatherPojo.class);
//    }
//    @GetMapping("/getByCity3")
//    public WeatherPojo getWeatherByCity3(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
//        //使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问
//        ServiceInstance serviceInstance = loadBalancerClient.choose("weather");
//        String url = String.format("http://%s:%s/getByCity?id=%s",serviceInstance.getServiceId(),serviceInstance.getPort(),id);
//        System.out.println("request url:"+url);
//        return restTemplate.getForObject(url,WeatherPojo.class);
//    }
}
